<?php
require_once(dirname(__FILE__) . '/../../inc/config.inc.php');
require_once(dirname(__FILE__) . '/../../function.php');
require_once(dirname(__FILE__) . '/../../../vendor/autoload.php');

// 使用 PHPSpreadsheet 命名空间
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Style\Color;


$dtime = isset($_GET['dtime']) ? htmlspecialchars($_GET['dtime']) : "";   //用车人
$dtime = trim($dtime);
$task = isset($_GET['task']) ? intval($_GET['task']) : 0;   //商家id
$manageid = isset($_GET['manageid']) ? intval($_GET['manageid']) : 0;   //用户的身份

$data = [];

$sql = "SELECT *  FROM `#@__task` where checkinfo=1 and is_delete=0  order by classid asc  ";


$new_item = [];
$top_title = "";
$dosql->Execute($sql);
$i=1;
while ($row = $dosql->GetArray()) {



    $row['idd'] =$i;
    $row['typename'] = getTypeName($row['classid']);

    if($row['unit']!=""){
        $row['value'].=" / ".$row['unit'];
    }

    $row_arr['data'][] = $row;
    $i++;
    $new_item[] = $row;

}

//echo $sql;
/*var_dump($new_item);
exit;*/

$cellKey = array(
    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
    'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
    'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM',
    'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ'
);
//序号	省	市	区	报修单位	联系人	联系方式	设备类型	序列号	故障描述	维修进度	维修费用	报修时间	客服联系备注	完修时间	维修概要	完修工程师	备注 报修地址 报修地址说明  完修地址 完修地址说明  报修到完修距离 报修到服务站距离
$spreadsheet = new Spreadsheet();// 创建新的电子表格对象
$sheet = $spreadsheet->getActiveSheet();// 获取活动工作表
$sheet->setTitle("服务管理类别及评分标准");// 设置表格标题

$cellName = array(
    'id' => array('序号', 'idd', 8), //列名称，索引值，宽度
    'typename' => array('类别', 'typename', 18), //列名称，索引值，宽度
    'taskname' => array('项目名称', 'title', 30), //列名称，索引值，宽度
    'fee' => array('计分标准', 'value', 12),
    'remark' => array('备注', 'remark', 30),

);


$topNumber = 2;//表头有几行占用

$styleThinBlackBorderOutline = array(
    'borders' => array(
        'allBorders' => array( //设置全部边框
            'borderStyle' => Border::BORDER_THIN //细边框
        ),
    ),
);


// 设置第一行
$spreadsheet->getDefaultStyle()->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);//垂直居中
$sheet->mergeCells('A1:' . $cellKey[count($cellName) - 1] . '1');
//合并单元格（如果要拆分单元格是需要先合并再拆分的，否则程序会报错）
$sheet->setCellValue('A1', $top_title . "服务管理类别及评分标准");
$sheet->getStyle('A1')->getFont()->setBold(true);
$sheet->getStyle('A1')->getFont()->setSize(20);
$sheet->getRowDimension('1')->setRowHeight(50);
$sheet->getStyle('A1')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
$sheet->getStyle('A1')->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);
$sheet->getStyle('A1')->getAlignment()->setWrapText(true);


//$objPHPExcel->getActiveSheet()->getStyle('T')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
//处理表头

$cellName_num = count($cellName);
$lastCell = $cellKey[$cellName_num - 1]; //获取最后一行的 excel 字母

$i = 0;
foreach ($cellName as $k => $v) {

    $sheet->setCellValue($cellKey[$i] . $topNumber, $v[0]);//设置表头数据
    $sheet->getStyle($cellKey[$i] . $topNumber)->getFont()->setBold(true);//设置是否加粗
    $sheet->getColumnDimension($cellKey[$i])->setWidth($v[2]);//设置列宽度
    //添加边框
    $sheet->getStyle($cellKey[$i] . $topNumber)->applyFromArray($styleThinBlackBorderOutline);
    //设置字体 为10号
    $sheet->getStyle($cellKey[$i] . $topNumber)->getFont()->setSize(10);
    // 设置所有垂直居中
    $sheet->getStyle($cellKey[$i] . $topNumber)->getAlignment()
        ->setVertical(Alignment::VERTICAL_CENTER);

    // 居中
    $sheet->getStyle($cellKey[$i] . $topNumber)->getAlignment()
        ->setHorizontal(Alignment::HORIZONTAL_CENTER);
    $sheet->getRowDimension($topNumber)->setRowHeight(24);
    $i++;
}
//处理数据
foreach ($new_item as $k => $v) {
    // 当前行数值
    $now_line = $k + 1 + $topNumber;
    $ii = 0;
    foreach ($cellName as $k1 => $v1) {
        $sheet->setCellValue($cellKey[$ii] . $now_line, $v[$v1[1]]);
        $sheet->getStyle($cellKey[8] . $now_line)->getFont()->getColor()->setARGB(Color::COLOR_RED);// 设置文字颜色
        $ii++;
    }

    // 设置所有垂直居中

    $sheet->getStyle('A' . $now_line . ':' . $lastCell . $now_line)->getAlignment()
        ->setVertical(Alignment::VERTICAL_CENTER);
    // 居中
    $sheet->getStyle('A' . $now_line . ':' . $lastCell . $now_line)->getAlignment()
        ->setHorizontal(Alignment::HORIZONTAL_CENTER);
    //添加边框
    $sheet->getStyle('A' . $now_line . ':' . $lastCell . $now_line)->applyFromArray($styleThinBlackBorderOutline);
    $sheet->getStyle('A' . $now_line . ':' . $lastCell . $now_line)->getFont()->setSize(12);

    $sheet->getRowDimension($now_line)->setRowHeight(27);


//    $sheet->getStyle('H' . $now_line)->getAlignment()->setWrapText(true);
//    $sheet->getStyle('M' . $now_line)->getAlignment()->setWrapText(true);


}

$xlsTitle ='服务管理类别及评分标准';
ob_end_clean();
//清除缓冲区,避免乱码
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header("Content-Disposition: attachment; filename=$xlsTitle.xlsx");
header('Cache-Control: max-age=0');
//header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
//header ('Cache-Control: cache, must-revalidate');
header('Pragma: public');
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output');


exit
?>
